var config = require('../../../config');
const userdefuldUrl = config.userdefuldUrl;
const adrcUrl = config.adrcUrl;

var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    "address":1
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(this.data.address);
    // if(this.data.address==1){
    //   wx.redirectTo({
    //     url: '/pages/myinfo/site/site_select/site_select?id=1'
    //   })
    // }

    var that = this;


    app.getUserOpenId(function (err, openid) {
      if (!err) {
          wx.request({
            url: userdefuldUrl + openid, //仅为示例，并非真实的接口地址  
            success: function (res) {

              var int = {};
              int['userName'] = res.data.username;
              int['telNumber'] = res.data.telnumber;
              int['provinceName'] = res.data.provincename;
              int['cityName'] = res.data.cityname;
              int['countyName'] = res.data.countyname;
              int['detailInfo'] = res.data.detailinfo;

              if (res.data) {
                that.setData({
                  adr: int,
                  adrid: res.data.id,
                  ardid: res.data.id
                })
              }
            }
          })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  //更换收货地址
  adrup: function () {
    var that = this;
    wx.chooseAddress({
      success: function (res) {
        var adr = res;
        console.log(adr);
        app.getUserOpenId(function (err, openid) {
          if (!err) {
            wx.request({
              url: adrcUrl,
              data: {
                userid: openid,
                username: res.userName,
                telnumber: res.telNumber,
                provincename: res.provinceName,
                cityname: res.cityName,
                countyname: res.countyName,
                detailinfo: res.detailInfo,
              },
              method: 'post',
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
                console.log(res);
                that.setData({
                  adr: adr,
                  adrid: res.data.id
                })
              }
            });
          }
        })
      },
    })
  }

})